<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/taglib.jsp"%>
<jsp:include page="/WEB-INF/common/head.jsp" flush="true" />
<script type="text/javascript" src="${res_url}js/menu.js"></script>
<style type="text/css">
body{
	background-color: #ffffff;
}
.left{
	position: absolute;
	top: 10px;
	left: 20px;
	width: 358px;
	border-right: 1px solid #CCC;
	overflow-y: auto;
}

.tree-menu{
	float: right;
}
.tree-menu span{
	margin-left: 6px;
}
.tree-menu span i{
	cursor: pointer;
}
.icon-plus {
	background-position: -408px -96px;
}
.icon-remove {
	background-position: -312px 0;
}
.icon-edit {
	background-position: -96px -72px;
}
[class^="icon-"], [class*=" icon-"] {
	display: inline-block;
	width: 14px;
	height: 14px;
	line-height: 14px;
	vertical-align: text-top;
	background-image: url("${basePath}res/bootstrap/img/glyphicons-halflings.png");
	background-repeat: no-repeat;
	margin-top: 1px;
}
#menu_tree{
	margin-right: 20px;
}

li{
	line-height: 16px;
}
.om-tree-node a{
	display: inline-block;
	*display: inline;
	*zoom: 1;
	width: 115px;
	overflow: hidden;
	text-overflow: ellipsis;
}
#vip_tip{
	text-align: center;
}
.actions{
	position: absolute;
	bottom: 20px;
	left: 10px;
	width: 368px;
	border-right: 1px solid #CCC;
	height: 60px;
}
</style>
 
	<div  class="easyui-layout" id = "main_layout" style="width:100%;height:100%;">
		<input id="roleId" type="hidden" value="${roleId}"/>
		<div id="west" class="scrollbar" data-options="region:'west',split:true,collapsedSize:0" title="East"  style="width:400px;">
		<a href="#" class="easyui-linkbutton addBtn" iconCls="glyphicon-plus" id="addGroup"  plain="true">增加</a>
        <a href="#" class="easyui-linkbutton updateBtn" iconCls="glyphicon-pencil" id="editGroup" plain="true" >编辑</a>
        <a href="#" class="easyui-linkbutton delBtn" iconCls="glyphicon-remove"  id="deleteGroup" plain="true" >删除分组</a>
        <a href="#" class="easyui-linkbutton delBtn" iconCls="glyphicon-user"  id="addUser" plain="true" >添加用户</a>
        <a href="#" class="easyui-linkbutton delBtn" iconCls="glyphicon-remove-sign"  id="deleteUser" plain="true" >删除用户</a>
	       	<ul id="treeDemo" class="ztree"></ul>
		</div>
	
		<div data-options="region:'center',title:'用户信息'">
		<a href="#" class="easyui-linkbutton delBtn" iconCls="glyphicon-remove"  id="btn-unvisible" plain="true" ">删除用户</a>
		<a href="#" class="easyui-linkbutton addBtn" iconCls="glyphicon-plus" id="btn-add"  plain="true">增加用户</a>
        <a href="#" class="easyui-linkbutton updateBtn" iconCls="glyphicon-pencil" id="btn-edit" plain="true" >编辑用户</a>
        <a href="#" class="easyui-linkbutton delBtn" iconCls="glyphicon-user"  id="bnt-grant" plain="true" >分配角色</a>
        <a href="#" class="easyui-linkbutton delBtn" iconCls="glyphicon-remove-sign"  id="bnt-params" plain="true" >参数更新</a>
<!--         <a href="#" class="easyui-linkbutton delBtn" iconCls="glyphicon-remove-sign"  id="bnt-obj" plain="true" >对象查看</a> -->
        <a href="#" class="easyui-linkbutton delBtn" iconCls="glyphicon-remove-sign"  id="btn-site" plain="true" >分配站点</a>
		<table id="dg"></table>
		</div>
		</div>
		
<script type="text/javascript">
var datagrid = $("#dg");
var zNodes = "";
var treeOnfousId = "";
var treeOnfousType = "";
var grid_selector = "#grid-table";
var pager_selector = "#grid-pager";

var dgOptions = {
        rownumbers:true,
    	fit:true,
    	border:false,
        rownumbers:true,
    	url:'${context_path}/sys/user/getListData',
    	method:'post',
    	toolbar:'#tb',
    	pageSize: 20,
    	pagination:true,
    	multiSort:true,
        queryParams: getInitParam(),
        
        columns: [[
		          {field:'id', checkbox:true}
	           	 ,{field:'name', title: '用户名', width:120, sortable: true}
	           	 ,{field:'chinese', title: '拼音', width:120, sortable: true}
	           	 ,{field:'roleNames', title: '拥有角色', width:250, sortable: true}
	           	 ,{field:'des', title: '备注', width:100, sortable: true}
	        	 ,{field:'params', title: '参数', width:100, sortable: true}
	        	 ,{field:'site_name', title: '拥有站点', width:100, sortable: true}
	        	 ,{field:'createdate', title: '注册时间', width:100, sortable: true}
	           ]]
	};
	

function getInitParam() {
	var param = {};
	$("#tb :input[name]").each(function(i, item) {
		if($(item).val()) {
			param["queryParams[" + $(item).attr("name") + "]"] = $(item).val();	
		}
	});
	return param;
}
	
	
$(function() {
	//handleAuthDataRule();
	datagrid.datagrid(dgOptions);
	var roleId=$("#roleId").val();
	
	if(roleId!=null&&roleId.indexOf("2")>=0&&roleId.indexOf("1")<0){
	//	$("#btn-site").hide();
		$("#bnt-grant").hide();
	}

	initTree();
	zTree_Menu = $.fn.zTree.getZTreeObj("treeDemo");
});

$(document).ready(function(){  
    var height1 = $(window).height()-20;  
    $("#main_layout").attr("style","width:100%;height:"+height1+"px");  
    $("#main_layout").layout("resize",{  
        width:"100%",  
        height:height1+"px"  
    });  
});  
  
  
$(window).resize(function(){  
    var height1 = $(window).height()-30;  
    $("#main_layout").attr("style","width:100%;height:"+height1+"px");  
    $("#main_layout").layout("resize",{  
        width:"100%",  
        height:height1+"px"  
    });  
});   

function initTree(){

	$.ajax({  
        type : "post",  
         url : "${context_path}/sys/group/refreshTree",  
         async : false,  
         success : function(data){  
        	 zNodes = JSON.parse(data.data);
         }  
    }); 
	$.fn.zTree.init($("#treeDemo"), setting, zNodes);
}

var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			beforeCheck: beforeCheck,
			onClick: zTreeOnClick
		}
	};
	
function beforeCheck(treeId, treeNode) {
	return (treeNode.doCheck !== false);
}

function zTreeOnClick(event, treeId, treeNode) {
	treeOnfousId = treeNode.objId;
	treeOnfousType = treeNode.type;  
    if(treeNode.type=="group"){
    	datagrid.datagrid({url:"${context_path}/sys/group/getGroupListData?id="+treeNode.objId});
    }else if(treeNode.type=="root"){
    	datagrid.datagrid({url: "${context_path}/sys/user/getListData"});
 	 }else if(treeNode.type=="user"){
 		datagrid.datagrid({url: "${context_path}/sys/user/getUsertData?id="+treeNode.userId});
  }
};


function add() {
	top.window.subPage.loadCurrDatagrid = function() {
		showMsg("增加成功！");
		datagrid.datagrid("load");
	}
	top.openWindow("对象管理-增加", '${context_path}/sys/obj/add', {size: '600x400'});
}

function update() {
	var rowsSel = datagrid.datagrid("getSelections");
	if(rowsSel.length != 1) {
		showWarnMsg("请选择需要编辑的一条数据！");
		return;
	}
	var id = rowsSel[0].id;
	top.window.subPage.loadCurrDatagrid = function() {
		showMsg("更新成功！");
		datagrid.datagrid("reload");
	}
	top.openWindow("对象管理-编辑", '${context_path}/sys/obj/add?id=' + id, {size: '600x400'});
}

function del() {
	var ids = "";
	$.each(datagrid.datagrid("getSelections"), function(i, item) {
		ids = ids+item.id+",";
	});
	if(ids.length < 1) {
		showWarnMsg("请选择需要删除的数据！");
		return;
	}
	confirmMsg("确认删除？", function() {
		$.post("${context_path}/sys/obj/delete", {"ids": ids}, function(data) {
			showMsg("删除成功！");
			datagrid.datagrid("reload");
		});
	});
}



//按钮事件

/**
 * 打开子页
 */
var _openSubPageFlag = false;
function _openSubPage(pageUrl) {
	if(!_openSubPageFlag) {
		$("#subLayout").layout("expand", "east");
		setTimeout(function() {
			$("#subIframe").attr("src", pageUrl);
		}, 600);
		_openSubPageFlag = true;
	}else {
		$("#subIframe").attr("src", pageUrl);
	}
}

function forword(url){
	window.location.href=url;
}
//分组管理
/**
 * 加载分组树
 */
function loadTree() {
	$.post("/Portal/getMenu", function(data) {
		var menulist = [];
		$.each(data, function(i, item) {
			menulist.push({
				id: item.id,
				text: item.menu_name,
				attributes:"/Portal"+item.menu_url, 
				parent_id: item.parent_id,
				iconCls: item.icon
			});
		});
		
		for(var i = 0; i < menulist.length; i++) {
			var children = menulist[i].children || [];
			for(var j = 0; j < menulist.length; j++) {
				if(menulist[j].parent_id == menulist[i].id) {
					children.push(menulist[j]);
				}
			}
			menulist[i].children = children;
		}
		
		var treeData = [];
		$.each(menulist, function(i, item) {
			if(item.parent_id == 0) {
				treeData.push(item);
			}
		});
		$("#menuTree").tree({
			data: treeData,
			onBeforeSelect: treeBeforeSelect, 
			onSelect: treeSelect
		});
	});
}

//添加分组
$("#addGroup").click(function(){
	var pid
	if(treeOnfousType == "root"){
		pid = -1;			
	}else{
		pid = treeOnfousId;		
	}
	top.window.subPage.loadCurrDatagrid = function() {
		initTree();
		datagrid.datagrid("load");
		showMsg("增加成功！");
		
	}
	top.openWindow("用户管理-添加分组", '${context_path}/sys/group/add?pid='+pid, {size: '600x400'});

});
//删除分组
$("#deleteGroup").click(function(){
		deleteGroup(treeOnfousId,treeOnfousType);
});
//添加用户
$("#addUser").click(function(){
	    var gid
		if(treeOnfousType == "root"){
			showMsg("root 节点不能添加用户");		
		}else if(treeOnfousType == "user"){
			showMsg("请选择用户组节点");		
		}else{
			
			gid = treeOnfousId;
			top.window.subPage.loadCurrDatagrid = function() {
				initTree();
				datagrid.datagrid("load");
				showMsg("增加成功！");
				
			}
			top.openWindow("用户管理-添加用户", '${context_path}/sys/group/getZtree?gid='+gid, {size: '600x400'});
		}
	});
//删除分组用户
$("#deleteUser").click(function(){
    var gid
	if(treeOnfousType == "root"){
		showMsg("请选择用户节点");		
	}if(treeOnfousType == "group"){
		showMsg("请选择用户节点");		
	}else{
		selectIds = treeOnfousId;		
		var submitData={
				"users":selectIds,
		}		
		$.post("${context_path}/sys/group/deleteGroupUser" , submitData , 
			function(data){				
				initTree();
				datagrid.datagrid("load");
				showMsg("保存成功");
		},"json");
	}
});
//编辑分组
$("#editGroup").click(function(){
	var gid
	if(treeOnfousType == "root"){
		showMsg("root 节点不能编辑");		
	}if(treeOnfousType == "user"){
		showMsg("请选择用户组 节点");		
	}else{
		gid = treeOnfousId;		
		
		top.window.subPage.loadCurrDatagrid = function() {
			initTree();
			datagrid.datagrid("load");
			showMsg("增加成功！");
			
		}
		top.openWindow("用户管理-编辑分组",'${context_path}/sys/group/add?gid='+gid,{size: '600x400'});
	}

});




//删除分组
function deleteGroup(id,type){

	if(treeOnfousType == "root"){
		showMsg("请选择目录节点");		
	}if(treeOnfousType == "user"){
		showMsg("请选择目录节点");		
	}else{
	var submitData = {
			"id" : id,
			"type" : type
	};
	$.post("${context_path}/sys/group/delete", submitData,function(data) {
		if (data.code == 0) {
			initTree();
			showMsg("操作成功");
		}  else{
			showMsg(data.msg);
		} 
	},"json");
	}
}

/**
 * 用户管理
 */
 $("#btn-add").click(function(){//添加页面
	 
	 top.window.subPage.loadCurrDatagrid = function() {
			initTree();
			datagrid.datagrid("load");
			showMsg("增加成功！");
			
		}
		top.openWindow("用户管理-添加用户",'${context_path}/sys/user/add', {size: '600x400'});
	});
	
	//给用户分配角色
	$("#bnt-grant").click(function(){
		var rowsSel = datagrid.datagrid("getSelections");
		if(rowsSel.length != 1) {
			showWarnMsg("请选择需要编辑的一条数据！");
			return;
		}else {
			var id = rowsSel[0].id;
			top.window.subPage.loadCurrDatagrid = function() {
				showMsg("更新成功！");
				datagrid.datagrid("reload");
			}
			top.openWindow("用户管理-分配角色", '${context_path}/sys/user/userRoleSetting?uid='+id, {size: '600x400'});
		}
	});
	//删除用户
	$("#btn-unvisible").click(function(){
		deleteUser();
	});
	//设置用户参数
	$("#bnt-params").click(function(){
		var rowsSel = datagrid.datagrid("getSelections");
		if(rowsSel.length != 1) {
			showWarnMsg("请选择需要编辑的一条数据！");
			return;
		}else {
			var id = rowsSel[0].id;
			top.window.subPage.loadCurrDatagrid = function() {
				showMsg("更新成功！");
				datagrid.datagrid("reload");
			}
			top.openWindow("用户管理-设置参数", '${context_path}/sys/user/setParams?uids='+id, {size: '600x400'});		
		}
});
	//查看对象
	$("#bnt-obj").click(function(){
		var rowsSel = datagrid.datagrid("getSelections");
		if(rowsSel.length != 1) {
			showWarnMsg("请选择需要编辑的一条数据！");
			return;
		}else {
			var id = rowsSel[0].id;
			top.window.subPage.loadCurrDatagrid = function() {
				showMsg("更新成功！");
				datagrid.datagrid("reload");
			}
			top.openWindow("用户管理-查看对象信息", '${context_path}/sys/user/objInfo?id='+id, {size: '800x400'});		
		}

	});
	//编辑用户
	$("#btn-edit").click(function(){//添加页面
		var rowsSel = datagrid.datagrid("getSelections");
		if(rowsSel.length != 1) {
			showWarnMsg("请选择需要编辑的一条数据！");
			return;
		}else {
			var id = rowsSel[0].id;
			top.window.subPage.loadCurrDatagrid = function() {
				showMsg("更新成功！");
				datagrid.datagrid("reload");
			}
			top.openWindow("用户管理-编辑用户", '${context_path}/sys/user/add?id='+id, {size: '600x400'});		

		}
	});
	
	//编辑分配站点
	$("#btn-site").click(function(){//添加页面
		var rowsSel = datagrid.datagrid("getSelections");
		var roleNames = rowsSel[0].des;
		if(rowsSel.length != 1) {
			showWarnMsg("请选择需要编辑的一条数据！");
			return;
		}else{
			var id = rowsSel[0].id;
			top.window.subPage.loadCurrDatagrid = function() {
				showMsg("更新成功！");
				datagrid.datagrid("reload");
			}
			top.openWindow("用户管理-站点分配", '${context_path}/sys/site/siteUserSetView?id='+id, {size: '600x400'});		

		}
	});
	
	//删除用户
	function deleteUser(){
		var ids = "";
		$.each(datagrid.datagrid("getSelections"), function(i, item) {
			ids = ids+item.id+",";
		});
		if(ids.length < 1) {
			showWarnMsg("请选择需要删除的数据！");
			return;
		}
		confirmMsg("确认删除？", function() {
			$.post("${context_path}/sys/user/delete", {"ids": ids}, function(data) {
				initTree();
				datagrid.datagrid("load");
				showMsg("删除成功！");
			});
		});
	}

//扩展js
</script>
  <jsp:include page="/WEB-INF/common/dialogWindow.jsp" flush="true" />
   <jsp:include page="/WEB-INF/common/foot.jsp" flush="true" />